﻿using AutoMapper;
using Compass.PurchaseService.Domain;
using Compass.PurchaseService.Domain.Entities;
using Compass.PurchaseService.Infrastructure;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Purchases;

namespace Compass.Wasm.Server.Services.Purchases;
public interface INavItemService : IBaseService<NavItemDto>
{
    Task<ApiResponse<NavItemDto>> GetSingleByNumberAsync(string number);
    Task<ApiResponse<IEnumerable<NavItemDto>>> GetAllByFsAndShareAsync();
}
public class NavItemService : INavItemService
{
    #region ctor
    private readonly PurchaseDbContext _dbContext;
    private readonly IPurchaseRepository _repository;
    private readonly IMapper _mapper;
    public NavItemService(PurchaseDbContext dbContext, IPurchaseRepository repository, IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    }

    #endregion

    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<NavItemDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetNavItemsAsync();
            //按照料号排序
            var dtos = await _mapper.ProjectTo<NavItemDto>(models).OrderBy(x => x.Number).ToListAsync();
            return new ApiResponse<IEnumerable<NavItemDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<NavItemDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavItemDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetNavItemByIdAsync(id);
            if (model == null) return new ApiResponse<NavItemDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<NavItemDto>(model);
            return new ApiResponse<NavItemDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavItemDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavItemDto>> AddAsync(NavItemDto dto)
    {
        try
        {
            var model = new NavItem(Guid.NewGuid(), dto.Number.ToUpper(), dto.Description, dto.EnDescription, dto.Unit, dto.Sbu, dto.Group, dto.Consumable, dto.SafetyStock, dto.Inventory, dto.UnitCost, dto.LastCost, dto.UnitPrice, dto.Vendor,dto.OpenPo);
            await _dbContext.NavItems.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<NavItemDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavItemDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavItemDto>> UpdateAsync(Guid id, NavItemDto dto)
    {
        try
        {
            var model = await _repository.GetNavItemByIdAsync(id);
            if (model == null) return new ApiResponse<NavItemDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<NavItemDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavItemDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<NavItemDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetNavItemByIdAsync(id);
            if (model == null) return new ApiResponse<NavItemDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<NavItemDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavItemDto> { Status = false, Message = e.Message };
        }
    }


    #endregion
    
    #region 扩展查询
    public async Task<ApiResponse<NavItemDto>> GetSingleByNumberAsync(string number)
    {
        try
        {
            var model = await _repository.GetNavItemByNumberAsync(number);
            if (model == null) return new ApiResponse<NavItemDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<NavItemDto>(model);
            return new ApiResponse<NavItemDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<NavItemDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<IEnumerable<NavItemDto>>> GetAllByFsAndShareAsync()
    {
        try
        {
            var models = await _repository.GetNavItemsByFsAndShareAsync();
            //按照料号排序
            var dtos = await _mapper.ProjectTo<NavItemDto>(models).OrderBy(x => x.Number).ToListAsync();
            return new ApiResponse<IEnumerable<NavItemDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<NavItemDto>> { Status = false, Message = e.Message };
        }
    }

    #endregion
}